<template>
  <div>
    <el-dialog
      :title="ruleForm.id?'编辑权限':'添加权限'"
      :visible="visperm"
      width="50%"
      @close="celbtn"
    >
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-form-item label="权限名称" prop="title">
          <el-input v-model="ruleForm.title" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="ruleForm.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="ruleForm.description" type="textarea" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="celbtn">取 消</el-button>
        <el-button type="primary" @click="addbtn">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>
<script>
import { postperm, getputperm, putperm } from '@/api/permission'
export default {
  props: {
    visperm: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 表单数据
      ruleForm: {
        title: '',
        code: '',
        description: '',
        pid: '',
        type: ''// 权限类型1级2级
      },
      rules: {
        title: [
          { required: true, message: '请输入权限名称', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入权限标识', trigger: 'blur' }
        ], description: [
          { required: true, message: '请输入权限描述', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 确定按钮
    addbtn() {
      this.$refs.ruleForm.validate(async(valid) => {
        if (valid) {
          if (this.ruleForm.id) {
            // 编辑权限
            await putperm(this.ruleForm)
            //   提示
            this.$message({
              message: '添加权限成功',
              type: 'success'
            })
          } else {
            // 添加权限
            await postperm(this.ruleForm)
            //   提示
            this.$message({
              message: '添加权限成功',
              type: 'success'
            })
          }

          //   关闭弹窗
          this.celbtn()
          //   重新渲染
          this.$emit('getlist')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },

    // 编辑 获取当前点击权限详情
    async  getpermdetail(id) {
      const res = await getputperm(id)
      this.ruleForm = res.data
    },
    celbtn() {
      //   关闭弹窗
      this.$emit('update:visperm', false)
      this.$refs.ruleForm.resetFields()
      this.ruleForm = {
        title: '',
        code: '',
        description: '',
        pid: '',
        type: ''// 权限类型1级2级
      }
    }
  }
}
</script>
<style>

</style>
